
package LoadFromDatabase;

import fu.edbs.EDBSKeys;
import java.sql.ResultSet;
import java.sql.Statement;

public class LoadLinksFromDB extends Thread {

    private Statement statement;
    private String location;
    
    public LoadLinksFromDB(Statement statement, String location){
        this.statement = statement;
        this.location = location;
    }

    @Override
    public void run(){
    	String data = "";
        try{
            System.out.println("Start Loading Links");
            ResultSet resultSet = statement.executeQuery("SELECT id,crossing_id_from,crossing_id_to,meters,lsiclass,gao_id FROM link WHERE "+location+" ORDER BY id");
            System.out.println("Finished Loading Links");
            System.out.println("Start Writing File Links");
            while(resultSet.next()){
                int db_id = (int)resultSet.getLong(1);
                int db_crossing_id_from = (int)resultSet.getLong(2);
                int db_crossing_id_to = (int)resultSet.getLong(3);
                int db_meters = (int)resultSet.getLong(4);
//               int db_lsiclass = (int)resultSet.getLong(5);
                String gao_id = resultSet.getString(6);
//                String lsiKeyStr = EDBSKeys.lsiclasskey.get(new Integer(db_lsiclass));
                data += db_id + " " + db_crossing_id_from + " " + db_crossing_id_to + " " + db_meters + " " + gao_id + "\r\n";
            }
            resultSet.close();
        }catch(Exception ex){
            System.out.println("Error in Execute Link: "+ex.toString());
            ex.printStackTrace();
            System.exit(1);
        }
        WriteData.write(data, FileNames.fileNameLinks);
        System.out.println("Finished Writing File Links");
    }
}
